local BigNum = require "BigNum"

-------------------------------------------------------------------------------
---! 内部方法
-------------------------------------------------------------------------------
local function find_item_config(itemid)
    if not itemid then
        return
    end

    local items = IMPORT_D:query("item")
    if not items then
        return
    end

    local item = items[itemid]
    if not item then
        return
    end

    return item
end

---! 元表对象
local ITEM_META = {}

function ITEM_META:__index(key)
    local item = find_item_config(self.id)
    if not item then
        return
    end

    return item[key]
end

-------------------------------------------------------------------------------
---! 对外接口
-------------------------------------------------------------------------------
ITEM_D = {}

---! 查找道具对象
function ITEM_D:find_item(itemid)
    if not find_item_config(itemid) then
        return
    end

    return setmetatable({ id = itemid, }, ITEM_META)
end

---! 构建道具对象
function ITEM_D:clone_item(dbase)
    local item = setmetatable({}, ITEM_META)
    return table.update(item, dbase)
end

---! 初始道具数据
function ITEM_D:init_item_info(player)
    ----todo:
end

---! 还原道具数据
function ITEM_D:restore_item_dbase(player, item_dbase)
    if not item_dbase then
        return
    end

    local items = {}
    for key, dbase in pairs(item_dbase) do
        items[key] = ITEM_D:clone_item(dbase)
    end

    player:set_temp("item", items)
end

---! 获取道具数据
function ITEM_D:query_item_dbase(player)
    local items = player:query_temp("item")
    if not items then
        return
    end

    local item_dbase = {}
    for key, item in pairs(items) do
        item_dbase[key] = table.copy(item)
    end
    return item_dbase
end
